﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.PC.库存管理
{
    /// <summary>
    /// 库存管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = "PC")]
    [ApiController]
    public class InventoryController : BaseController
    {
        private readonly IInventoryService inventoryService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="inventoryService"></param>
        public InventoryController(IWebHostEnvironment environment, IInventoryService inventoryService) : base(environment)
        {
            this.inventoryService = inventoryService;
        }

        /// <summary>
        /// 库存明细
        /// </summary>
        /// <param name="whId"></param>
        /// <param name="aid"></param>
        /// <param name="mname"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryDetailList(string? ModelSpec, string? StockName, string? mname, int status = 0, int pageIndex=1, int pageSize=8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = inventoryService.GetInventoryDetailList(ModelSpec,StockName, mname, status,pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 库存汇总
        /// </summary>
        /// <param name="whId"></param>
        /// <param name="aid"></param>
        /// <param name="mname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryGroupList(long? whId = null, long? aid = null, string? mname = "", int pageIndex = 1, int pageSize=8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = inventoryService.GetInventoryGroupList(whId, aid, mname, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据物料编码获取库存信息（PDA使用）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryPalletDto(string? key, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var res = inventoryService.GetInventoryPalletDto(key, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(res, "200", totalCount));
        }


        /// <summary>
        /// 根据物料编码获取库存信息（PDA回流入库使用）
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetPickingShippingOrderDetail(string? key = "", int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var res = inventoryService.GetPickingShippingOrderDetail(key,pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(res, "200", totalCount));
        }

        /// <summary>
        /// 根据物料编码获取库存信息（PDA补货使用）
        /// </summary>
        /// <param name="key"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInventoryPalletDto5(string? key, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var res = inventoryService.GetInventoryPalletDto5(key, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(res, "200", totalCount));
        }

        /// <summary>
        /// 导出库存明细
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DowLoadExcel(long aid = 0,int status = 0)
        {
            var list = inventoryService.GetInventoryExport(aid,status);
            if (list != null)
                return BaseDowLoadExcel(list);
            else
                return Ok();
        }
    }
}
